package com.frxuexi.lib_base.utils

import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.R
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

object GlideUtils {


    fun loadImage(url:String,iv:ImageView){
        Glide.with(iv).load(url)
            .error(com.frxuexi.lib_base.R.drawable.ic_launcher_background)
            .placeholder(com.frxuexi.lib_base.R.drawable.ic_launcher_background)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .into(iv)
    }

    fun loadCircleImage(url:String,iv:ImageView){
        Glide.with(iv).load(url)
            .error(com.frxuexi.lib_base.R.drawable.ic_launcher_background)
            .placeholder(com.frxuexi.lib_base.R.drawable.ic_launcher_background)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .circleCrop()
            .into(iv)
    }

    fun loadRoundImage(url:String,iv:ImageView){
        Glide.with(iv).load(url)
            .error(com.frxuexi.lib_base.R.drawable.ic_launcher_background)
            .placeholder(com.frxuexi.lib_base.R.drawable.ic_launcher_background)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .apply(RequestOptions.bitmapTransform(RoundedCorners(20)))
            .into(iv)
    }
}

@BindingAdapter("url")
fun bindImage(iv: ImageView,url: String?){
    Glide.with(iv).load(url)
        .error(com.frxuexi.lib_base.R.drawable.ic_launcher_background)
        .placeholder(com.frxuexi.lib_base.R.drawable.ic_launcher_background)
        .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
        .apply(RequestOptions.bitmapTransform(RoundedCorners(20)))
        .into(iv)
}